package service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.toplink.SessionFactory;

import dao.tHistoryDao;
import dao.impl.tHistoryDaoImpl;

import entity.tHistory;
import service.tHistoryService;
import util.DBUtils;
import util.HibernateUtil;
import util.UUIDUtil;

public class tHistoryServiceImpl extends HibernateDaoSupport implements
		tHistoryService {
	private static Connection sqlServerConn = null;
	private tHistoryDao thistoryDao;

	/*
	 * static Resource resource=new ClassPathResource("applicationContext.xml");
	 * 
	 * static BeanFactory factory=new XmlBeanFactory(resource);
	 * 
	 * static SessionFactory sessionFactory =
	 * (SessionFactory)factory.getBean("sessionFactoryOracle");
	 */
	public void add(Integer mark, String hData, String deviceId) {
		if (mark != null && hData != null && deviceId != null) {
			String id = UUIDUtil.createUUID();
			DateFormat format = new SimpleDateFormat("yyyyMM");
			String dDate = format.format(new Date()) + "01";
			StringBuffer sb = new StringBuffer();
			sb.append("INSERT INTO tHistory").append(dDate).append(
					"(id,mark,hData,deviceId,hTime)  VALUES('").append(id).append(
					"',").append(mark).append(",'").append(hData).append("','")
					.append(deviceId).append("',GETDATE())");
			System.out.println("sb:" + sb.toString());

			// tHistoryDao th=new tHistoryDaoImpl();
			// th.add(sb.toString());
			// Session session =
			// getHibernateTemplate().getSessionFactory().getCurrentSession();
			try {
			sqlServerConn = DBUtils.getSqlServerConn();
			Statement sqlServerPs = sqlServerConn.createStatement();
			sqlServerPs.executeUpdate(sb.toString());
			sqlServerPs.close();
			System.out.println("添加成功");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void delete() {
		// TODO Auto-generated method stub
		this.thistoryDao.delete();

	}

	public List<tHistory> search() {
		// TODO Auto-generated method stub
		return this.thistoryDao.search();

	}

	public void update() {
		// TODO Auto-generated method stub
		this.thistoryDao.update();

	}

	public tHistoryDao getThistoryDao() {
		return thistoryDao;
	}

	public void setThistoryDao(tHistoryDao thistoryDao) {
		this.thistoryDao = thistoryDao;
	}

}
